package com.tang.campus_market.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.tang.campus_market.entity.Goods;
import com.tang.campus_market.entity.Order;
import com.tang.campus_market.service.GoodsService;
import com.tang.campus_market.service.OrderService;
import com.tang.campus_market.util.PageResult;
import com.tang.campus_market.util.Result;
import com.tang.campus_market.util.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @ Author     ：Marine.Li
 * @ Date       ：Created in 14:17 2021/8/5
 * @ Description：订单控制层
 * @ Modified By：
 * @Version: 1.0.0$
 */

@RestController
@RequestMapping("/order")
@CrossOrigin
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    GoodsService goodsService;

    // 查询全部订单
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public @ResponseBody
    Result getOrderList(@RequestBody Map<String, Object> map) {
        List<Order> orders = orderService.queryList(map);
        return new Result(StatusCode.OK, "成功", orders);
    }

    // 分页查询订单
    @RequestMapping(value = "/selectPage", method = RequestMethod.POST)
    public @ResponseBody
    Result getOrderPage(
            @RequestParam(value = "page", required = true, defaultValue = "1") Integer page,
            @RequestParam(value = "rows", required = true, defaultValue = "5") Integer rows) {
        Page<Order> orderPage = orderService.selectOrderPage(page, rows);
        PageResult<Order> orderResult = new PageResult<Order>(
                orderPage.getTotal(), orderPage.getRecords()
        );
        return new Result(StatusCode.OK, "订单查询成功", orderResult);
    }

    // 通过orderId查询订单
    @RequestMapping(value = "/{orderId}", method = RequestMethod.GET)
    public @ResponseBody
    Result getOrder(@PathVariable("orderId") Integer orderId) {
        Order order = orderService.getById(orderId);
        return new Result(StatusCode.OK, "成功", order);
    }

    // 通过顾客id查询所有订单｛暂无分页｝
    @RequestMapping(value = "/customerId/{customerId}", method = RequestMethod.GET)
    public @ResponseBody
    Result getOrderByCustomerId(@PathVariable("customerId") Integer customerId) {
        List<Order> orders = orderService.getOrderByCustomerId(customerId);
        Collections.reverse(orders);
        return new Result(StatusCode.OK, "成功", orders);
    }

    // 通过卖家id查询所有订单｛暂无分页｝
    @RequestMapping(value = "/sellerId/{sellerId}", method = RequestMethod.GET)
    public @ResponseBody
    Result getOrderBySellerId(@PathVariable("sellerId") Integer sellerId) {
        List<Order> orders = orderService.getOrderBySellerId(sellerId);
        return new Result(StatusCode.OK, "成功", orders);
    }

    // 暂时不用
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public @ResponseBody
    Result getOrderInfo(@RequestParam(value = "orderId", required = true) Integer orderId) {
        Order order = orderService.getById(orderId);
        return new Result(StatusCode.OK, "成功", order);
    }

    // 获取顾客除当前订单外的其他订单｛最多显示4-5个｝
    @RequestMapping(value = "/customer/others", method = RequestMethod.GET)
    public @ResponseBody
    Result getCustomerOtherOrder(@RequestParam(value = "orderId", required = true) Integer orderId,
                                 @RequestParam(value = "customerId", required = true) Integer customerId) {
        List<Order> orders = orderService.getCustomerOtherOrders(customerId, orderId);
        return new Result(StatusCode.OK, "成功", orders);
    }

    // 获取卖家除当前订单外的其他订单｛最多显示4-5个｝
    @RequestMapping(value = "/seller/others", method = RequestMethod.GET)
    public @ResponseBody
    Result getSellerOtherOrder(@RequestParam(value = "orderId", required = true) Integer orderId,
                               @RequestParam(value = "sellerId", required = true) Integer sellerId) {
        List<Order> orders = orderService.getSellerOtherOrders(sellerId, orderId);
        return new Result(StatusCode.OK, "成功", orders);
    }

    // 取消当前订单
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping(value = "/cancel/{goodId}&{orderId}", method = RequestMethod.GET)
    public @ResponseBody
    Result cancelOrder(@PathVariable("goodId") Integer goodId, @PathVariable("orderId") Integer orderId) {
        //updateGoodState(1,goodId);
        Integer integer = orderService.cancelOrder(goodId, orderId);
        if(integer==1){
            Order order = orderService.selectById(orderId);
            goodsService.updateGoodSaleState("0",order.getGoodId());
            return new Result(StatusCode.OK, "成功", integer);
        }else{
            throw new RuntimeException("订单删除失败");
        }
    }

    // 更新订单状态
    @RequestMapping(value = "update/status/{orderId}&{statusId}", method = RequestMethod.POST)
    public @ResponseBody
    Result updateOrderStatus(@PathVariable("orderId") Integer orderId,
                             @PathVariable("statusId") Integer statusId) {
        orderService.updateOrder(orderId, statusId);
        return new Result(StatusCode.OK, "成功", orderService.getById(orderId));
    }

    // 创建订单
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public @ResponseBody
    Result createOrder(@RequestBody Order order) {
        Integer integer = orderService.createOrder(order);
        if (integer != 1) {
            throw new RuntimeException("订单插入数目异常，可能是未插入或多次插入");
        } else {
            Goods good = goodsService.findById(order.getGoodId());
            if(!"0".equals(good.getSaleState())){
                throw new RuntimeException("重复下单");
            }
            goodsService.updateGoodSaleState("2", order.getGoodId());
            return new Result(StatusCode.OK, "成功", order.getId());
        }
    }

    // 完成订单
    @RequestMapping(value = "/complete/{orderId}", method = RequestMethod.POST)
    public @ResponseBody
    Result complete(@PathVariable Integer orderId) {
        Integer complete = orderService.complete(orderId);
        return new Result(StatusCode.OK, "成功", complete);
    }

    // 删除订单
    @RequestMapping(value = "/delete/{orderId}", method = RequestMethod.DELETE)
    public @ResponseBody
    Result delete(@PathVariable Integer orderId) {
        int delete = orderService.delete(orderId);
        return new Result(StatusCode.OK, "成功", delete);
    }
}
